package com.byedu.course;

import com.byedu.base.BaseService;

import com.byedu.domain.course.AdsDO;

import com.byedu.domain.course.vo.AdsVO;
import com.byedu.mapper.course.AdsMapper;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenxigui on 2017/4/26.
 */
@Service
public class AdsService extends BaseService {
	
    @Autowired
    private AdsMapper adsMapper;

    /**
     * @description 获取专题列表
     * @author chenxigui
     * @date 2017-04-26
     * @return
     */
    public List<AdsVO> listCanUseAds() {
        return convertDoListToVoList(adsMapper.listCanUseAds());
    }
    public List<AdsDO> listAds() {
        return adsMapper.listAds();
    }

    public int delAds(int id) {
        return adsMapper.delAds(id);
    }
    public AdsDO getAdsById(int id) {
        return adsMapper.getAdsById(id);
    }


    public int saveAds(AdsDO adsDO){
         return adsMapper.insertAds(adsDO);
    }

    public List<AdsVO> convertDoListToVoList(List<AdsDO> adsDOList) {
        if (CollectionUtils.isNotEmpty(adsDOList)) {
            List<AdsVO> adsVOList = new ArrayList<AdsVO>();
            for (AdsDO adsDO : adsDOList) {

                try {
                    AdsVO adsVO = new AdsVO();
                    BeanUtils.copyProperties(adsVO, adsDO);
                    adsVOList.add(adsVO);
                } catch (Exception e) {
                    logger.info("广告信息转换错误", e);
                }
            }
            return adsVOList;
        }

        return null;
    }

    public int updateAds(AdsDO adsDO) {
            return adsMapper.updateAds(adsDO);
    }
}
